CommandHandler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 34
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A parseSlashCommand 0 5 3
B process 0 24 7
1
import NewPollFormCard from '../cards/NewPollFormCard';
2
import {chat_v1 as chatV1} from '@googleapis/chat';
3
import BaseHandler from './BaseHandler';
4
import {buildOptionsFromMessage} from '../helpers/utils';
5
import {generateHelpText} from '../helpers/helper';
6
7
export default class CommandHandler extends BaseHandler {
8
  private slashCommand: chatV1.Schema$SlashCommandMetadata | undefined;
9
10
  public constructor(event: chatV1.Schema$DeprecatedEvent) {
11
    super(event);
12
    this.parseSlashCommand();
13
14
    if (this.slashCommand === undefined) {
15
      throw new Error('No Slash Command found');
16
    }
17
  }
18
19
  parseSlashCommand() {
20
    this.getAnnotations().forEach((annotation) => {
21
      if (annotation.type === 'SLASH_COMMAND') {
22
        this.slashCommand = annotation.slashCommand!;
23
      }
24
    });
25
  }
26
27
  process(): chatV1.Schema$Message {
28
    switch (this.slashCommand!.commandName) {
29
      case '/poll':
30
        const argumentText = this.event.message?.argumentText?.trim() ?? '';
31
        const options = buildOptionsFromMessage(argumentText);
32
        return {
33
          actionResponse: {
34
            type: 'DIALOG',
35
            dialogAction: {
36
              dialog: {
37
                body: new NewPollFormCard(options, this.getUserTimezone()).create(),
38
              },
39
            },
40
          },
41
        };
42
      default:
43
        const isPrivate = this.event.space?.type === 'DM';
44
        return {
45
          thread: this.event.message!.thread,
46
          actionResponse: {
47
            type: 'NEW_MESSAGE',
48
          },
49
          text: generateHelpText(isPrivate),
50
        };
51
    }
52
  }
53
}
54